/**
 * 
 */
package com.yunyike.controller.user;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.OrderConstants;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.dto.UserOrdersDTO;
import com.yunyike.exception.BadRequestException;
import com.yunyike.model.OrderMerchandiseMapping;
import com.yunyike.model.OrderRecepientMapping;
import com.yunyike.model.User;
import com.yunyike.service.IOrderMerchandiseMappingService;
import com.yunyike.service.IOrderRecepientMappingService;
import com.yunyike.service.IOrderService;

@Controller
@RequestMapping(value=RequestConstant.USER_ORDER)
public class UserOrderController extends BaseController {

	private Logger log = LoggerFactory.getLogger(UserOrderController.class);
	
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IOrderRecepientMappingService recepientmapService;
	
	@Autowired
	private IOrderMerchandiseMappingService merchandiseMappingService;
	
	@RequestMapping(value=RequestConstant.USER_ORDER_SHOW+"/{orderId}"+RequestConstant.MERCHANDISE_SUFFIX)
	public String showUserOrder(@PathVariable String orderId,
			HttpServletRequest request, HttpServletResponse response,Model model){
		log.info("controller showUserOrder...");
		if(StringUtils.isBlank(orderId)){
			log.info("controller showUserOrder request param not correct.");
			throw new BadRequestException();
		}
		log.info("orderID==="+orderId);
		User user = getCurrentUser();
		UserOrdersDTO dto = new UserOrdersDTO();
		dto.setUserId(user.getUserId());
//		dto.setOrderStatus(orderStatus);
		dto.setOrderId(orderId);
		List<UserOrdersDTO> userOrders = orderService.findUserOrders(dto);
		UserOrdersDTO userOrder = userOrders.get(0);
		userOrder.setBoxNum(1);
		BigDecimal totalCount = userOrder.getOrderPrice().divide(new BigDecimal("0.01"),BigDecimal.ROUND_HALF_UP);
		model.addAttribute("totalCount", totalCount.intValue());
		OrderRecepientMapping recepient = recepientmapService.findOrderRecepient(orderId);
		List<OrderMerchandiseMapping>  merchandiseList= merchandiseMappingService.findMerchandiseByOrderId(orderId);
		model.addAttribute("recepient", recepient);
		model.addAttribute("userOrder", userOrder);
		model.addAttribute("merchandiseList", merchandiseList);
		
		if(userOrder.getOrderStatus()==0){
			return ResponseConstant.USER_IMMEDIATE_PAYMENT;
		}
		
		return ResponseConstant.USER_PACKAGE_LIST;
		
	}
	
	@ResponseBody
	@RequestMapping(value=RequestConstant.USER_ORDER_MODIFY_STATUS+"/{orderId}/{status}")
	public String modifyOrderStatus(@PathVariable String orderId,@PathVariable Integer status){
		log.info("controller modifyOrder...");
		if(StringUtils.isBlank(orderId)){
			log.info("controller modifyOrderStatus request param not correct.");
			throw new BadRequestException();
		}
		log.info("orderID==="+orderId);
		User user = getCurrentUser();
		UserOrdersDTO dto = new UserOrdersDTO();
		dto.setUserId(user.getUserId());
		dto.setOrderId(orderId);
		dto.setOrderStatus(status);
		String result = orderService.updateOrderStatu(dto,status);
		log.info("ontroller modifyOrder..end.result==="+result);
		return result;
	}
	/**
	 * 根据条件获取订单
	 * @param status
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value=RequestConstant.USER_ORDER_FIND+"/{status}")
	public String findUserOrder(@PathVariable Integer status,
			HttpServletRequest request, HttpServletResponse response,Model model){
		log.info("...findUserOrder...");
		/**********status*订单状态******
		 * 0:（已提交）待支付，
		 * 1:已支付（待发货）,2:已发货（待出口），3:已出口,4:已到岸， 5:已进口清关， 6:已国内配送（待收货），---> -1:待收货
		 * 7:已收货				  
		 * ************************/
		User user = getCurrentUser();
		UserOrdersDTO dto = new UserOrdersDTO();
		dto.setUserId(user.getUserId());
		dto.setOrderStatus(status);
		
		if(status==100)dto.setOrderStatus(null);//全部订单
		
		List<UserOrdersDTO> userOrders = orderService.findUserOrders(dto);
		model.addAttribute("userOrders", userOrders);
		model.addAttribute("status", status);
		model.addAttribute("ORDER_STATUS_MAP", OrderConstants.getOrderStatusMap());
		return ResponseConstant.USER_ORDERS;
	}
	/**
	 * 查询当前订单的商品
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value=RequestConstant.USER_ORDER_MERCHANDISES+"/{orderId}")
	public String findOrderSettlementItems(@PathVariable(value="orderId") String orderId, Model model){
		
		if(StringUtils.isBlank(orderId)){
			throw new BadRequestException("request param not correct.");
		}
		
		log.info("findSettlementItemsByorderId... orderId:"+orderId);
		
		
		List<OrderMerchandiseMapping>  merchandiseList= merchandiseMappingService.findMerchandiseByOrderId(orderId);
		
		model.addAttribute("merchandiseList", merchandiseList);
		log.info("findSettlementItemsByorderId end.");
		
		return ResponseConstant.USER_ORDER_MERCHANDISES;
	}
	
}
